import BackButton from "@/components/BackButton";
import TabBar from "@/components/TabBar";
import React, { Suspense, useEffect } from "react";
import { Outlet, useLocation, useNavigate } from "react-router-dom";
import useLoginState from "@/hooks/useLoginState";
import Loading from "@/components/Loading";
import eventBus from "@/utils/eventBus-ts";

export default function App() {
  // const navigate = useNavigate();
  // const { pathname } = useLocation();
  // // 直接在这里进行token的判断
  // let token = localStorage.getItem("token");
  // useEffect(() => {
  //   if (token) {
  //     if (pathname === "/") {
  //       navigate("/home");
  //     }
  //   } else {
  //     navigate("/login");
  //   }
  // });

  useLoginState("/", "/home");

  // 在App 里面进行 登录超时的订阅
  const navigate = useNavigate();
  eventBus.on("logintimeout", () => {
    navigate("/login");
  });
  return (
    <div className="app">
      {/* 所有懒加载的组件，都需要一个 suspense 的组件先进行占位 */}
      <Suspense fallback={<Loading />}>
        <Outlet />
      </Suspense>
      <TabBar />
      <BackButton />
    </div>
  );
}
